package com.longdaji.decoration.ui.activitiesOfMine.pendingEvaluated;

import com.longdaji.decoration.Constants;
import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.bean.Orders;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.utils.LogUtil;
import com.longdaji.decoration.utils.RxUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Kanade on 2018/3/8 0008.
 */

public class PendingEvaluatedPresenter extends RxPresenter<PendingEvaluatedContract.View> implements PendingEvaluatedContract.Presenter {
    private DataManager mDataManager;
    private Account account;

    @Inject
    public PendingEvaluatedPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
        account = Account.getInstance();
    }


    @Override
    public void initData() {
        doGetOrder();

    }

    private void doGetOrder() {
        final List<Orders.OrdersBean.OrderBean> pendingEvaluatedOrderList = new ArrayList<>();
        addSubscribe(mDataManager.doGetOrderApi(account.getUserid(), null, null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<Orders>handleResult())
                .subscribeWith(new CommonSubscriber<Orders>() {
                    @Override
                    public void onSuccess(final Orders orders) {
                        LogUtil.d("succeed : doGetOrder ");
                        Observable.create(new ObservableOnSubscribe<List<Orders.OrdersBean.OrderBean>>() {
                            @Override
                            public void subscribe(ObservableEmitter<List<Orders.OrdersBean.OrderBean>> e) throws Exception {
                                for (Orders.OrdersBean.OrderBean orderBean : orders.getOrders().getOrder()) {
                                    // TODO: 2018/4/9 0009 这里只是暂定为5，等接口改出来再看
                                    if (Constants.ORDER_FINISHED == orderBean.getOrderStatus()) {
                                        pendingEvaluatedOrderList.add(orderBean);
                                    }
                                }
                                e.onNext(pendingEvaluatedOrderList);

                            }
                        }).subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Consumer<List<Orders.OrdersBean.OrderBean>>() {
                                    @Override
                                    public void accept(List<Orders.OrdersBean.OrderBean> pendingDeliveryOrderList) throws Exception {
                                        mView.showPendingEvaluatedOrders(pendingDeliveryOrderList);
                                    }
                                });

                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d("failed : " + msg);
                    }
                }));
    }

}
